package com.djl.rabbitmq.controller;

import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @program: rabbitmq-hello
 * @description: 发送消息
 * @author: Mr.dai
 * @create: 2021-09-14 16:04
 **/
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info("当前时间：{},发送一条信息给两个ttl队列:{}",new Date().toString(),message);
        rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s"+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s"+message);
    }
    //开始发消息
    @GetMapping("/send/{message}/{ttlTime}")
    public void send(@PathVariable String message,@PathVariable String ttlTime){
        log.info("当前时间：{},时长{},发送一条信息给一个ttl队列:{}",new Date().toString(),ttlTime,message);
        rabbitTemplate.convertAndSend("X","XC",message,(Message msg)->{
            //设置发送消息时候的演延时队列的时长
            msg.getMessageProperties().setExpiration(ttlTime);
            return msg;
        });

    }
}
